package com.jiayi.weiphoto.util;

import java.io.File;
import java.util.List;
import java.util.TimeZone;

import com.jiayi.weiphoto.activity.MainConstants;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.telephony.TelephonyManager;
import android.util.Log;

public class DeviceTool {
	
	public static final int ANDROID_1_5 = 3; 
	public static final int ANDROID_1_6 = 4; 
	public static final int ANDROID_2_0 = 5; 
	public static final int ANDROID_2_0_1 = 6; 
	public static final int ANDROID_2_1 = 7; 
	public static final int ANDROID_2_2 = 8; 
	public static final int ANDROID_2_3 = 9; 
	public static final int ANDROID_2_3_3 = 10; 
	public static final int ANDROID_3_0 = 11; 
	public static final int ANDROID_3_1 = 12;
	public static final int ANDROID_3_2 = 13;
	public static final int ANDROID_4_0 = 14;
	public static final int ANDROID_4_0_3 = 15;
	public static final int ANDROID_4_1 = 16;
	public static final int ANDROID_4_2 = 17;
	
	public static final int ROOT_STATUS_UNCHECK = -2;
	public static final int ROOT_STATUS_UNKNOWN = -1;
	public static final int ROOT_STATUS_FALSE= 0;
	public static final int ROOT_STATUS_TRUE = 1;
	
	private static final String TAG = "Env";
	private static final int SDK_LEVEL = Integer.parseInt(Build.VERSION.SDK);
	private static int rootStatus = ROOT_STATUS_UNCHECK;
	
	public static String getVersion(Context context) {
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			return info.versionName;
		} catch (NameNotFoundException e) {
			return "null";
		}
	}
	
	public static int getVersionCode(Context context) {
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			return info.versionCode;
		} catch (NameNotFoundException e) {
			return -1;
		}
	}
	
	public static String getAppFlag(Context context){
		String appFlag = "";
		try {
			ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
			appFlag = appInfo.metaData.getString("APP_FLAG");
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return appFlag;
	}
	
	public static String getVersionName(Context context){
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			return info.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	public static int getSDKLevel(){
		return android.os.Build.VERSION.SDK_INT;
	}
	
	public static String getSDK(){
		if(Build.VERSION.SDK != null)
			return Build.VERSION.SDK;
		return "null";
	}
	
	public static String getModels(){
		if(Build.MODEL != null)
		return Build.MODEL;
		return "null";
	}
	
	public static int getTimeZone(){
		TimeZone timezone=TimeZone.getDefault();
		return timezone.getOffset(System.currentTimeMillis());
	}
	
	public static String getCounty(Context context){
//	String locale = context.getResources().getConfiguration().locale.getCountry();  
//	String locale = context.getResources().getConfiguration().locale.getDisplayCountry(); 
	TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
	String countryCode = tm.getSimCountryIso(); 
	if(countryCode == null)
		countryCode = "null";
	return countryCode;
	}
	
	public static String getRom(){
		if(Build.BOARD != null)
		return Build.BOARD;
		return "null";
	}
	
	
	public static int getRootStatus() {
		if(rootStatus == ROOT_STATUS_UNCHECK) {
			String pathStr = System.getenv("PATH");
			String[] paths;
			
			if(pathStr == null) {
				paths = new String[] {"/sbin", "/system/sbin", "/system/bin", "/system/xbin"};
				rootStatus = ROOT_STATUS_UNKNOWN;
			} else {
				paths = pathStr.split(File.pathSeparator);
				rootStatus = ROOT_STATUS_FALSE;
			}
			
			for (String path : paths) {
				File suFile = new File(path + File.separatorChar + "su");
				try {
					if(suFile.isFile()) {
						rootStatus = ROOT_STATUS_TRUE;
						break;
					}
				} catch(SecurityException e) {
					Log.w(TAG, "", e);
					rootStatus = ROOT_STATUS_UNKNOWN;
				}
			}				
		}
		return rootStatus;
	}
	
	public static int getAppSourceState(Context context) throws SettingNotFoundException{
		int res = 0;
		res = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS);
		return res;
	}
	
	public static void installApk(String apkPath, Context context){
		Intent intent = new Intent(Intent.ACTION_VIEW);
		Uri uri = Uri.parse("file://" + apkPath);
		intent.setDataAndType(uri, "application/vnd.android.package-archive");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}
	
	public static boolean hasSDCard(){
		return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
	}
	
	public static String getMetaString(String key, Context context){
		String res = null;
		try {
			ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
			res = info.metaData.getString(key);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}
}
